package com.css.fxfzypg.modules.rating.library.dao;

import cn.hutool.core.date.DateUtil;
import com.css.fxfzypg.modules.rating.library.model.entity.KpExamScoringDb;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Slf4j
@Repository
public class KpExamScoringDbRepository {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
    public void insert(KpExamScoringDb kpExamScoringDb) {
        String sql = "INSERT INTO kp_exam_scoring_db(id, name, create_time, create_user, update_time, update_user, del_flag) values(:id, :name, :createTime, :createUser, :updateTime, :updateUser, :delFlag)";
        namedParameterJdbcTemplate.update(sql, new BeanPropertySqlParameterSource(kpExamScoringDb));
    }

    public KpExamScoringDb getById(String kpkId) {
        try {
            String sql = "select * from kp_exam_scoring_db where id = '" + kpkId + "' and del_flag = '0'";
            return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(KpExamScoringDb.class));
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    public List<KpExamScoringDb> list(KpExamScoringDb param) {
        String sql = "select * from kp_exam_scoring_db where del_flag = '0'";
        if (StringUtils.isNotBlank(param.getName())) {
            sql += " and name = '" + param.getName() + "' ";
        }
        if (ObjectUtils.isNotEmpty(param.getId())) {
            sql += " and id = ''" + param.getId() + "' ";
        }
        sql += " order by create_time desc";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(KpExamScoringDb.class));
    }

    public void deleteById(String kesdId, String delUid, Date delTime) {
        String sql = "update kp_exam_scoring_db set del_flag = '1', update_time = '" + DateUtil.format(delTime, "yyyy-MM-dd HH:mm:ss") + "', update_user = '" + delUid + "' where id = '" + kesdId + "' ";
        jdbcTemplate.update(sql);
    }
}
